﻿Imports DTO
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Data.SqlTypes
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.OleDb
Public Class LoaiPinDAO
    Public Shared Function ReadData() As List(Of LoaiPinDTO)
        Dim ketqua As New List(Of LoaiPinDTO)
        Dim data As New DataProvider()
        data.KetNoiCSDL()
        Dim command As New SqlCommand()
        command.Connection = data.connectionary
        command.CommandText = "SELECT * FROM LOAIPIN"
        Dim dt As DataTable = DataProvider.ExecQuery(command)
        For Each row As DataRow In dt.Rows
            Dim pin As New LoaiPinDTO(row.Item(0), row.Item(1), row.Item(2))
            ketqua.Add(pin)
        Next
        data.DongKetNoi()
        Return ketqua
    End Function
    Public Sub InsertData(ByVal tenloai As String, ByVal dongia As Integer)
        Dim data As New DataProvider()
        data.KetNoiCSDL()
        Dim command As New SqlCommand()
        command.Connection = data.connectionary
        command.CommandText = "INSERT INTO LOAIPIN (TenLoai, DonGia) VALUES (@TenLoai,@DonGia)"
        Try
            command.Parameters.AddWithValue("@TenLoai", tenloai)
            command.Parameters.AddWithValue("@DonGia", dongia)

            command.ExecuteNonQuery()
            data.DongKetNoi()
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            data.DongKetNoi()
        Finally
            data.DongKetNoi()
        End Try
    End Sub
    Public Sub DeleteData(ByVal maloai As Integer)
        Dim data As New DataProvider()
        data.KetNoiCSDL()
        Dim command As New SqlCommand()
        command.Connection = data.connectionary
        command.CommandText = "DELETE FROM LOAIPIN WHERE MaLoai = @MaLoai"
        Try
            command.Parameters.AddWithValue("@MaLoai", maloai)

            command.ExecuteNonQuery()
            data.DongKetNoi()
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            data.DongKetNoi()
        Finally
            data.DongKetNoi()
        End Try
    End Sub
    Public Sub UpdateData(ByVal maloai As Integer, ByVal tenloai As String, ByVal dongia As Integer)
        Dim data As New DataProvider()
        data.KetNoiCSDL()
        Dim command As New SqlCommand()
        command.Connection = data.connectionary
        command.CommandText = "UPDATE LOAIPIN SET TenLoai = @TenLoai, DonGia = @DonGia WHERE MaLoai = @MaLoai"
        Try
            command.Parameters.AddWithValue("@TenLoai", tenloai)
            command.Parameters.AddWithValue("@DonGia", dongia)
            command.Parameters.AddWithValue("@MaLoai", maloai)

            command.ExecuteNonQuery()
            data.DongKetNoi()
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            data.DongKetNoi()
        Finally
            data.DongKetNoi()
        End Try
    End Sub
End Class
